//dofile for batch of results I want to release in 2022

use "$temp/acs_longform_merged_clean", clear
keep if dby>=1990 //restrict to at-risk sample

tab age_group parent_inc_quart
replace coll = 0 if age_group == 1

ren state_2000 stfip
merge m:1 stfip using "$data/Other/state_region_xwalk", keep(match) nogen
ren stfip state_2000

ren cz temp
ren cz_2000 cz
merge m:1 cz using "$temp/cz_hpi_shocks"
ren cz cz_2000
ren temp cz
replace shock_hpi_2009 = shock_hpi_2009*-1
replace shock_hpi_2012 = shock_hpi_2012*-1
su shock_hpi_2009 [w=weight]
replace shock_hpi_2009 = `r(mean)' if shock_hpi_2009 == . //little fix here.


************year version
gen year_group = 0
replace year_group = 1 if year == 2005 | year == 2006 //2007/2008 is the omitted category
replace year_group = 2 if year == 2009 | year == 2010
replace year_group = 3 if year == 2011 | year == 2012
replace year_group = 4 if year == 2013 | year == 2014
replace year_group = 5 if year == 2015 | year == 2016
replace year_group = 6 if year == 2017 | year == 2018
gen year_dum_1 = (year_group == 1) * cz_shock
gen year_dum_2 = (year_group == 2) * cz_shock
gen year_dum_3 = (year_group == 3) * cz_shock
gen year_dum_4 = (year_group == 4) * cz_shock
gen year_dum_5 = (year_group == 5) * cz_shock
gen year_dum_6 = (year_group == 6) * cz_shock
replace coll = 0 if year_group == 0
replace coll = 0 if year_group == 1
replace coll = 0 if year_group == 2

forval q = 1/4{

	local controls `"cz_shock sex asian black hispan i.age i.state_2000#i.head_coll i.year_group head_mar head_constr_manu house_value parent_inc own_house"'
	//local controls `"sex asian black hispan i.age i.head_coll ib0.year_group#i.div head_mar head_constr_manu house_value parent_inc own_house"'
	reg coll year_dum* `controls' [aw = weight] if parent_inc_quart == `q', vce(cluster cz_2000)

	//graphing
	forval i = 1/6{
		local effect_`i'_`q' = _b[year_dum_`i']
		local se_`i'_`q' = _se[year_dum_`i']
		su coll [aw = weight] if year_group == `i' & parent_inc_quart == `q'
		local mean_`i'_`q' = `r(mean)'
	}
}

//set up data to make graph out of
preserve
clear
set obs 24
gen group = .
gen effect = .
gen se = .
gen mean = .
gen quart = .
forval q = 1/4{
	forval i = 1/6{
		local line = (`q'-1)*6 + `i'
		replace quart = `q' in `line'
		replace group = `i' in `line'
		replace effect = `effect_`i'_`q'' in `line'
		replace se = `se_`i'_`q'' in `line'
		replace mean = `mean_`i'_`q'' in `line'
	}
}
	
keep if group == 6	
gen effect_pct = (effect/mean)*100
gen se_pct = (se/mean)*100
serrbar effect_pct se_pct quart, scale(1.96) bgcolor(white) graphregion(color(white)) ///
ytitle("Percentage Effect, 2017-2018") xlabel(1 "1" 2 "2" 3 "3" 4 "4") xtitle("Parent Income Quartile") yline(0)
graph export "$disc/2_effect_coll_year.png", replace width(1500)
restore


